<template>
  <div>
    <x-card>
      <div slot="content" class="table-page-search-wrapper">
        <a-form layout="inline">
          <a-row :gutter="48">
            <a-col :md="8" :sm="24">
              <a-form-item label="项目名称">
                <a-select v-model="queryParam.project" allow-clear placeholder="请选择项目名称">
                  <a-select-option v-for="item in projectList" :key="item.name" :value="item.id"
                  >{{ item.name }}
                  </a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
              <span class="table-page-search-submitButtons">
                <a-button type="primary" @click="$refs.table.refresh(true)">查询</a-button>
                <a-button style="margin-left: 8px" @click="() => (queryParam = {})">重置</a-button>
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>
    </x-card>
    <a-card :bordered="false">
      <s-table
        ref="table"
        :columns="columns"
        :data="loadData"
        :rowKey="(record) => record.id"
      >
        <template slot="operator" v-if="hasPerm('channel:add')">
          <a-button @click="$refs.Form.open()" icon="plus" type="primary">新增渠道</a-button>
        </template>

        <span slot="action" slot-scope="text, record">
                    <a v-if="hasPerm('channel:edit')" @click="$refs.Form.open(record)">编辑</a>
                    <a-divider type="vertical" v-if="hasPerm('channel:edit') & hasPerm('channel:resetPwd')"/>
                    <a v-if="hasPerm('channel:resetPwd')" @click="resetPwd(record.id)">重置密码</a>
                    <a-divider type="vertical" v-if="hasPerm('channel:resetPwd') & hasPerm('channel:delete')"/>
                    <a-popconfirm
                      v-if="hasPerm('channel:delete')"
                      placement="topRight"
                      title="确认删除？"
                      @confirm="() => gameChannelDelete(record.id)"
                    >
                        <a style="color: red">删除</a>
                    </a-popconfirm>
                </span>
      </s-table>
      <s-form ref="Form" @ok="handleOk"/>
    </a-card>
  </div>
</template>
<script type="text/jsx">
  import { STable, XCard } from '@/components'
  import sForm from './Form'
  import * as api from '@/api/main/channel'
  import { getPage } from '@/api/main/project'

  export default {
    components: {
      XCard,
      STable,
      sForm
    },
    data() {
      return {
        advanced: false,
        // 查询参数
        queryParam: {},
        // 表头
        columns: [
          {
            title: '项目',
            dataIndex: 'project_name'
          },
          {
            title: '渠道简称',
            dataIndex: 'channel_api'
          },
          {
            title: '渠道全称',
            dataIndex: 'channel_name'
          }
          // {
          //     title: '数据库用户名',
          //     dataIndex: 'db_user',
          // },
          // {
          //     title: '数据库名',
          //     dataIndex: 'db_name',
          // },
          // {
          //     title: 'IP地址',
          //     dataIndex: 'host_ip',
          // },
          // {
          //     title: '数据库端口',
          //     dataIndex: 'db_port',
          // },
          // {
          //     title: 'SSH端口',
          //     dataIndex: 'ssh_port',
          // },
        ],
        // 加载数据方法 必须为 Promise 对象
        loadData: (parameter) => {
          return api.getPage(Object.assign(parameter, this.queryParam)).then((res) => {
            return res.data
          })
        },
        db_password: '',
        projectList: [],
        selectedRowKeys: [],
        selectedRows: [],
        defaultExpandedKeys: []
      }
    },
    /**
     * 相当于html的onload方法，进来初始化
     */
    created() {
      getPage().then((res) => {
        this.projectList = res.data.rows
      })
      if (this.hasPerm('channel:edit') || this.hasPerm('channel:delete') || this.hasPerm('channel:resetPwd')) {
        this.columns.push({
          title: '操作',
          width: '150',
          dataIndex: 'action',
          scopedSlots: { customRender: 'action' }
        })
      }
    },
    methods: {
      handleOk() {
        this.$refs.table.refresh()
      },
      /**
       * 重置密码
       */
      resetPwd(id) {
        let self = this
        this.$confirm({
          title: '重置数据库密码',
          icon: 'info-circle',
          content: this.resetPwdForm,
          onOk() {
            api.channelResetPwd({ id: id, db_password: self.db_password }).then((res) => {
              if (res.success) {
                self.$message.success('密码重置成功')
                self.db_password = ''
              } else {
                self.$message.error(self.message['failed'] + res.message)
              }
            })
          },
          onCancel() {
            console.log('Cancel')
          }
        })
      },
      resetPwdForm() {
        return (
          <div>
            <a-form-item>
              <a-input-password v-model={this.db_password}/>
            </a-form-item>
          </div>
        )
      },
      /**
       * 删除渠道
       */
      gameChannelDelete(id) {
        api.deleteObj(id).then((res) => {
          if (res.success) {
            this.$message.success('删除成功')
            this.$refs.table.refresh(true)
          } else {
            this.$message.error('删除失败：' + res.message)
          }
        })
      }
    }
  }
</script>
<style lang="less">
  button {
    margin-right: 8px;
  }

  .table-title-right {
    float: right;
    margin-right: 30px;
  }
</style>
